package com.eamois.dao.manual.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.dao.manual.EnsipStructureDao;
import com.eamois.domain.manual.Ensip;
import com.eamois.domain.manual.EnsipEntity;
import com.eamois.domain.manual.EnsipStructure;
import com.incors.plaf.alloy.cr;


public class EnsipStructureDaoImpl extends BaseDaoImpl<EnsipStructure> implements EnsipStructureDao{

	@SuppressWarnings("unchecked")
	public Collection<EnsipStructure> findEnsipStructrueByEntity(
			final EnsipEntity ensipEntity) {
		// TODO Auto-generated method stub
		return (Collection<EnsipStructure>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipStructure.class);
				criteria.add(Restrictions.eq("ensipEntity", ensipEntity));
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	public Collection<EnsipStructure> findEnsipStructrueByEnsip(final Ensip ensip) {
		// TODO Auto-generated method stub
		return (Collection<EnsipStructure>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipStructure.class);
				criteria.add(Restrictions.eq("ensip", ensip));
				return criteria.list();
			}
		});
	}

	@Override
	public EnsipStructure findEnsipStructrueByEntityAndEnsip(
			final EnsipEntity ensipEntity, final Ensip ensip) {
		// TODO Auto-generated method stub
		return (EnsipStructure)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipStructure.class);
				criteria.add(Restrictions.eq("ensip", ensip));
				criteria.add(Restrictions.eq("ensipEntity", ensipEntity));
				ArrayList<EnsipStructure> list = (ArrayList<EnsipStructure>)criteria.list();
				if (list.size() == 0) {
					return null;
				}else{
					return list.get(0);
				}
			}
		});
	}

}
